﻿using MediatR;
using SheepCultivation.Common.Domain.Week2;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;

namespace SheepCultivation.API.AddliCation.BuildingD
{
    public class BuildingCreateHandler : IRequestHandler<BuildingCreateCommon, Result>
    {
        #region 仓储
        /// <summary>
        /// 仓储
        /// </summary>
        public readonly IBaseRepository<Building> BuildingRepository;
        public readonly IBaseRepository<BuildingType> BuildingTypeRepository;
        public readonly ILogger<BuildingCreateHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="buildingRepository">栋舍信息</param>
        /// <param name="buildingTypeRepository">栋舍类型</param>
        /// <param name="logger">日志</param>
        public BuildingCreateHandler(IBaseRepository<Building> buildingRepository, IBaseRepository<BuildingType> buildingTypeRepository, ILogger<BuildingCreateHandler> logger)
        {
            BuildingRepository = buildingRepository;
            BuildingTypeRepository = buildingTypeRepository;
            this.logger = logger;
        }
        #endregion

        #region 栋舍新增
        /// <summary>
        /// 栋舍信息新增
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(BuildingCreateCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonEnumCode.error };
            //异常处理
            try
            {
                logger.LogInformation("栋舍信息新增成功!");
                var res = new Building()
                {
                    BuildingTypeId = request.BuildingTypeId,
                    BuildingName = request.BuildingName,
                    BuildingPerson = request.BuildingPerson,
                    BuildingStaff = request.BuildingStaff,
                    BuildingState = request.BuildingState,
                    BuildingRenamk = request.BuildingRenamk,
                };
                BuildingRepository.Add(res);

                result.Code = (int)CommonEnumCode.success;
                result.Msg = "栋舍信息新增成功";
            }
            catch (Exception ex)
            {
                logger.LogError("栋舍信息新增失败!" + ex.Message);
                result.Msg = "栋舍信息新增失败";

            }
            return result;
        }
        #endregion

    }
}
